package com.hunan.api.catv.service.impl.cps;

import com.google.gson.JsonObject;
import com.hunan.api.catv.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Service
public class CpsService {

    @Autowired
    RestTemplate restTemplate;

    public JsonObject getCustomerProductInfo(String domain, Map<String, String> body) {
        StringBuffer url = new StringBuffer(domain);
        url.append("/user/getCustomerProductInfo");
        HttpEntity<String> request = new HttpEntity<String>(GsonUtils.toJson(body), getHeader());
        ResponseEntity<String> response = restTemplate.postForEntity(url.toString(), request, String.class);
        if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
            return GsonUtils.fromJson(response.getBody(), JsonObject.class);
        }
        return null;
    }

    public JsonObject queryPaymentInfo(String domain, Map<String, String> body) {
        StringBuffer url = new StringBuffer(domain);
        url.append("/user/queryPaymentInfo");
        HttpEntity<String> request = new HttpEntity<String>(GsonUtils.toJson(body), getHeader());
        ResponseEntity<String> response = restTemplate.postForEntity(url.toString(), request, String.class);
        if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
            return GsonUtils.fromJson(response.getBody(), JsonObject.class);
        }
        return null;
    }

    public JsonObject queryUserInfo(String domain, Map<String, String> body) {
        StringBuffer url = new StringBuffer(domain);
        url.append("/user/queryUserInfo");
        HttpEntity<String> request = new HttpEntity<String>(GsonUtils.toJson(body), getHeader());
        ResponseEntity<String> response = restTemplate.postForEntity(url.toString(), request, String.class);
        if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
            return GsonUtils.fromJson(response.getBody(), JsonObject.class);
        }
        return null;
    }

    public JsonObject queryUserStatus(String domain, Map<String, String> body) {
        StringBuffer url = new StringBuffer(domain);
        url.append("/user/queryUserStatus");
        HttpEntity<String> request = new HttpEntity<String>(GsonUtils.toJson(body), getHeader());
        ResponseEntity<String> response = restTemplate.postForEntity(url.toString(), request, String.class);
        if (response.getStatusCodeValue() == HttpStatus.OK.value()) {
            return GsonUtils.fromJson(response.getBody(), JsonObject.class);
        }
        return null;
    }

    public HttpHeaders getHeader() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
        return headers;
    }
}
